package cn.wolfcode.ssm.web.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.wolfcode.ssm.domain.Department;
import cn.wolfcode.ssm.query.QueryObject;
import cn.wolfcode.ssm.service.IDepartmentService;

@Controller
@RequestMapping("department")
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;

    @RequestMapping("list")
    public String list(Model model, @ModelAttribute("qo") QueryObject qo) throws  Exception{

        model.addAttribute("result", departmentService.query(qo));

        return "department/list";
    }
    //主页面编辑
    @RequestMapping("input")
    public String input(Long id, Model model) throws  Exception{

        if(id != null){
            model.addAttribute("entity", departmentService.get(id));
        }

        return "department/input";
    }
    //添加与更新
    @RequestMapping("saveOrUpdate")
    public String saveOrUpdate(Department entity) throws  Exception{
        //更新操作数据已经存在，所以拥有id
        if(entity.getId() != null){
            departmentService.update(entity);
        }else{
            departmentService.save(entity);
        }
        return "redirect:/department/list.do";
    }
    //删除操作
    @RequestMapping("delete")
    public String input(Long id) throws  Exception{
        if(id != null){
            departmentService.delete(id);
        }
        return "redirect:/department/list.do";
    }
}
